package com.fangcun.service.api.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fangcun.bean.api.teahouse.response.TeahouseResponse;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.entity.Teahouse;
import com.fangcun.mapper.TeahouseMapper;
import com.fangcun.service.api.TeahouseApiService;
import com.github.pagehelper.PageHelper;

@Service
public class TeahouseApiServiceImpl implements TeahouseApiService{
	
	@Autowired
	private TeahouseMapper teahouseMapper;

	@Override
	public Teahouse detail(Teahouse request) throws ProException {
		if (StringUtils.isEmpty(request.getLatitude()) || StringUtils.isEmpty(request.getLongitude())) {
			throw new ProException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
		}
		PageHelper.startPage(1, 1);
		List<TeahouseResponse> list = teahouseMapper.apiDetail(request);
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		if (list.get(0).getAverage() != null) {
			list.get(0).setAverage(new BigDecimal(list.get(0).getAverage().toString()).setScale(1, BigDecimal.ROUND_HALF_UP).floatValue());
		} else {
			list.get(0).setAverage(5f);
		}
		return list.get(0);
	}
	
	
}
